<?php
include_once("mysql_connect.inc.php");
include_once("hash.php");

//=========================================================================================================================================
function showUserForm($submitname, $function)
{
   global $errmsg;

   $username = "";
   $fullname = "";

   if (isset ($_POST["username"]) && ($function == ADDFUNCTION))
   {
      $username = $_POST["username"];
      $fullname = $_POST["fullname"];
   }

   if (($function == EDITFUNCTION) || ($function == DELETEFUNCTION) || ($function == INQUIREFUNCTION))
   {
      $username = $_POST["username"];

      if (isset ($_POST["fullname"]))
      {
         $fullname = $_POST["fullname"];
      }
      else
      {
         $fullname = getUserNameFromDB();
      }
   }

   $formshow  = '<FORM METHOD="POST" ACTION="';
   
   if ($function == ADDFUNCTION)
   {
      $formshow .= 'adduser.php">';;
   }
   elseif ($function == EDITFUNCTION)
   {
      $formshow .= 'edituser.php">';;
   }
   elseif ($function == DELETEFUNCTION)
   {
      $formshow .= 'deluser.php">';;
   }
   else
   {
      $formshow .= 'inquser.php">';;
   }
   
   $formshow .= '<font color=red>'.$errmsg.'<br><br><br></font>';
   $formshow .= '<TABLE BORDER="0"><TR>';
   $formshow .= '<TD width="250">User name';
   
   if ($function == ADDFUNCTION || $function == EDITFUNCTION)
   {
      $formshow .= '<font color="red">*</font>';
   }

   $formshow .= '</TD>';

   $formshow .= '<TD><INPUT TYPE="TEXT" NAME="username" SIZE="25" value="'.$username.'" ';

   if ($function == ADDFUNCTION)
   {
      $formshow .= '></TD></TR>';
   }
   else
   {
      $formshow .= 'READONLY></TD></TR>';
   }
   $formshow .= '<TR><TD>Full Name';

   if ($function == ADDFUNCTION || $function == EDITFUNCTION)
   {
      $formshow .= '<font color="red">*</font>';
   }

   $formshow .= '</TD>';

   $formshow .= '<TD><INPUT TYPE="TEXT" NAME="fullname" SIZE="80" value="'.$fullname.'" ';

   if ($function == ADDFUNCTION || $function == EDITFUNCTION)
   {
      $formshow .= '></TD></TR>';
   }
   else
   {
      $formshow .= 'READONLY></TD></TR>';
   }
   
   if (($function == ADDFUNCTION) || ($function == EDITFUNCTION))
   {
      $formshow .= '<TR><TD>Password';

      if ($function == ADDFUNCTION)
      {
         $formshow .= '<font color="red">*</font>';
      }

      $formshow .= '</TD>';

      $formshow .= '<TD><INPUT TYPE="password" NAME="password" SIZE="26"></TD></TR>';
      $formshow .= '<TR><TD>Confirm Password';

      if ($function == ADDFUNCTION)
      {
         $formshow .= '<font color="red">*</font>';
      }

      $formshow .= '</TD>';
      
      $formshow .= '<TD><INPUT TYPE="password" NAME="confirmpassword" SIZE="26"></TD></TR>';
   }

   $formshow .= '</TABLE></br>';
   if($function == ADDFUNCTION || $function == EDITFUNCTION || $function == DELETEFUNCTION) {
		$formshow .= '<font size=2; color=red> * The following fields are Required</font>';
   }
   if (($errmsg != userSaved) && ($function != INQUIREFUNCTION))
   {
      $formshow .= '<P ALIGN=CENTER><BR><BR><INPUT TYPE="SUBMIT" VALUE="'.$submitname.'" NAME="submit"></P>';
   }
  
   $formshow .= '</FORM>';

   print $formshow;
}
//=========================================================================================================================================
function showUserList($submitname, $function)
{
   global $errmsg;

   $formshow  = '<FORM METHOD="POST" ACTION="';
   
   if ($function == EDITFUNCTION)
   {
      $formshow  .= 'edituser.php">';
   }
   elseif ($function == DELETEFUNCTION)
   {
      $formshow  .= 'deluser.php">';
   }
   else
   {
      $formshow  .= 'inquser.php">';
   }
   
   $formshow .= '<font color=red>'.$errmsg.'<br><br><br></font>';
   $formshow .= '<TABLE BORDER="0"><TR>';
   $formshow .= '<TD width="250">User name</font></TD>';
   $formshow .= '<TD><SELECT name="username">';
   $formshow .= UserListFromDBToSelect();
   $formshow .= '</SELECT></TD></TR>';
   $formshow .= '</TABLE>';
   $formshow .= '<P ALIGN=CENTER><BR><BR><INPUT TYPE="SUBMIT" VALUE="'.$submitname.'" NAME="submit"></P>';
   $formshow .= '</FORM>';

   print $formshow;
}
//=========================================================================================================================================
function validateUserForm($submitname)
{
   global $link;
   global $errmsg;
   
   $errmsg = '';

   if ((strlen($_POST["username"]) == 0) && ($submitname == ADDUSERSUBMITTEXT))
   {
      $errmsg = "Please enter user name.";
      return(false);
   }

   if (strlen($_POST["fullname"]) == 0)
   {
      $errmsg = "Please enter full name.";
      return(false);
   }

   if ((strlen($_POST["password"]) == 0) && ($submitname == ADDUSERSUBMITTEXT))
   {
      $errmsg = "Please enter password.";
      return(false);
   }

   if ($submitname == ADDUSERSUBMITTEXT || $submitname == EDITUSERSUBMITTEXT)
   {
      if ($_POST["password"] != $_POST["confirmpassword"])
      {
         $errmsg = "Password does not match.";
         return(false);
      }
   }

   return(true);
}

?>
